package com.bckj.fastboot.core.extra.tenant.configuration;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.bckj.fastboot.core.extra.tenant.BaseTenantHandler;
import com.bckj.fastboot.core.extra.tenant.TenantConfigProperties;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@ConditionalOnProperty(name = "fastboot.tenant.enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(TenantConfigProperties.class)
public class FastTenantConfiguration {

    @Resource
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Bean
    @ConditionalOnMissingBean
    public BaseTenantHandler baseTenantHandler(TenantConfigProperties tenantConfigProperties) {
        BaseTenantHandler baseTenantHandler = new BaseTenantHandler(tenantConfigProperties);
        TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
        tenantInterceptor.setTenantLineHandler(baseTenantHandler);
        mybatisPlusInterceptor.addInnerInterceptor(tenantInterceptor);
        return baseTenantHandler;
    }

}
